package cn.csc.module.pay.controller.admin.fund;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.math.BigInteger;
import java.util.*;
import java.io.IOException;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.common.pojo.CommonResult;
import static cn.csc.framework.common.pojo.CommonResult.success;

import cn.csc.framework.excel.core.util.ExcelUtils;

import cn.csc.framework.operatelog.core.annotations.OperateLog;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.csc.module.pay.controller.admin.fund.vo.*;
import cn.csc.module.pay.dal.dataobject.fund.PayFundDO;
import cn.csc.module.pay.convert.fund.PayFundConvert;
import cn.csc.module.pay.service.fund.PayFundService;

@Api(tags = "管理后台 - 用户资金")
@RestController
@RequestMapping("/pay/fund")
@Validated
public class PayFundController {

    @Resource
    private PayFundService fundService;

    @PostMapping("/create")
    @ApiOperation("创建用户资金")
    @PreAuthorize("@ss.hasPermission('pay:fund:create')")
    public CommonResult<BigInteger> createFund(@Valid @RequestBody PayFundCreateReqVO createReqVO) {
        return success(fundService.createFund(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户资金")
    @PreAuthorize("@ss.hasPermission('pay:fund:update')")
    public CommonResult<Boolean> updateFund(@Valid @RequestBody PayFundUpdateReqVO updateReqVO) {
        fundService.updateFund(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户资金")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('pay:fund:delete')")
    public CommonResult<Boolean> deleteFund(@RequestParam("id") BigInteger id) {
        fundService.deleteFund(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户资金")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('pay:fund:query')")
    public CommonResult<PayFundRespVO> getFund(@RequestParam("id") BigInteger id) {
        PayFundDO fund = fundService.getFund(id);
        return success(PayFundConvert.INSTANCE.convert(fund));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户资金列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('pay:fund:query')")
    public CommonResult<List<PayFundRespVO>> getFundList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<PayFundDO> list = fundService.getFundList(ids);
        return success(PayFundConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户资金分页")
    @PreAuthorize("@ss.hasPermission('pay:fund:query')")
    public CommonResult<PageResult<PayFundRespVO>> getFundPage(@Valid PayFundPageReqVO pageVO) {
        PageResult<PayFundDO> pageResult = fundService.getFundPage(pageVO);
        return success(PayFundConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户资金 Excel")
    @PreAuthorize("@ss.hasPermission('pay:fund:export')")
    @OperateLog(type = EXPORT)
    public void exportFundExcel(@Valid PayFundExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PayFundDO> list = fundService.getFundList(exportReqVO);
        // 导出 Excel
        List<PayFundExcelVO> datas = PayFundConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户资金.xls", "数据", PayFundExcelVO.class, datas);
    }

}
